<?php

/**
 * This is the model class for table "tbl_relatedlink".
 *
 * The followings are the available columns in table 'tbl_relatedlink':
 * @property integer $relatedlink_id
 * @property integer $type
 * @property string $title
 * @property string $url
 * @property string $description
 * @property integer $status
 */
class Relatedlink extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Relatedlink the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_relatedlink';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('type, title, url, img', 'required'),
			array('type, status', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>200),
			array('url', 'length', 'max'=>200),
			array('description', 'length', 'max'=>500),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('relatedlink_id, type, title, url, description, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                                                    'pic' => array(self::BELONGS_TO, 'Resources', 'img'),
		);
    }

    const TYPE_SCHOOL_IN = 1;
    const TYPE_SCHOOL_OUT = 0;

    /**
     * Returns the array for relatedlink type.
     * @return array of type
     * @author nonglei
     * @date  15/08/2014
     */
    public function getTypeOptions()
    {
        return array (
            self::TYPE_SCHOOL_IN => '校内链接',
            self::TYPE_SCHOOL_OUT => '校外链接',
        );
    }
    public function getTypeText()
    {
        $a = array (
            self::TYPE_SCHOOL_IN => '校内链接',
            self::TYPE_SCHOOL_OUT => '校外链接',
        );
	return $a[$this->type];
    }
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'relatedlink_id' => '友情链接',
			'type' => '链接类型',
			'title' => '友链名称',
			'url' => 'Url',
			'description' => '描述',
			'status' => 'Status',
			'img' => 'Logo',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('relatedlink_id',$this->relatedlink_id);
		$criteria->compare('type',$this->type);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}
